package com.zhongwang.mms.module.basic.service;

import com.zhongwang.mms.gen.entity.MMaterialDictionary;
import com.zhongwang.mms.gen.entity.MMaterialDictionaryExample;
import com.zhongwang.mms.gen.entity.MMaterialsExample;
import com.zhongwang.mms.module.basic.dao.MMaterialDictionaryDao;
import com.zhongwang.mms.module.basic.dao.MMaterialsDao;
import com.zhongwang.mms.module.basic.model.MMaterialDictionaryListModel;
import com.zhongwang.mms.module.basic.model.MMaterialDictionaryModel;
import com.zhongwang.mms.module.system.log.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 添加原料数据字典 服务层
 * @Author: China.wsg
 * @Date: 2020/03/16 11:23
 */
@Service
public class MMaterialDictionaryService {

    @Autowired
    private MMaterialDictionaryDao mMaterialDictionaryDao;
    @Autowired
    private LogService logService;
    @Autowired
    private MMaterialsDao mMaterialsDao;

    /**
     * 自动获取数据字典编码
     *
     * @param typeNum 类别编号
     * @return
     */
    public String getMaterialCode(Integer typeNum) {

        Integer digit = 2;
        if (typeNum == 3000) {
            digit = 3;
        }

        MMaterialDictionaryExample ex = new MMaterialDictionaryExample();
        ex.setOrderByClause("md_mat_code desc");
        MMaterialDictionaryExample.Criteria criteria = ex.createCriteria();

        // 类别编号
        criteria.andMdTypeNumEqualTo(typeNum);
        List<MMaterialDictionary> mMaterialDictionaries = mMaterialDictionaryDao.selectByExample(ex);

        String materialCode;
        if (mMaterialDictionaries != null && mMaterialDictionaries.size() > 0) {
            materialCode = String.format("%0" + digit + "d", Integer.parseInt(mMaterialDictionaries.get(0).getMdMatCode()) + 1);
        } else {
            materialCode = String.format("%0" + digit + "d", 1);
        }
        return materialCode;
    }

    /**
     * 根据条件查询物料数据字典
     *
     * @param materialDictionary 查询条件
     * @return 原料数据字典列表
     */
    public List<MMaterialDictionaryModel> findMaterialDictionaryByQuery(MMaterialDictionary materialDictionary) {
        return mMaterialDictionaryDao.findMaterialDictionaryByQuery(materialDictionary);
    }

    /**
     * 保存原料物料数据字典
     *
     * @param materialDictionary
     * @return
     */
    public String saveMaterialDictionary(MMaterialDictionary materialDictionary) {

        // 判断 内容和编码 不允许重复
        MMaterialDictionaryExample ex = new MMaterialDictionaryExample();
        MMaterialDictionaryExample.Criteria criteria = ex.createCriteria();

        criteria.andMdPidEqualTo(materialDictionary.getMdPid());
        criteria.andMdTypeNumEqualTo(materialDictionary.getMdTypeNum());
        criteria.andMdNameEqualTo(materialDictionary.getMdName());
        criteria.andMdStatusEqualTo(1);
        List<MMaterialDictionary> materialDictionary1 = mMaterialDictionaryDao.selectByExample(ex);

        if (materialDictionary1 != null && materialDictionary1.size() > 0) {
            return "内容：" + materialDictionary.getMdName() + "在字典中已存在，请重新输入！";
        }

        // 判断 内容和编码 不允许重复
        ex.clear();
        ex = new MMaterialDictionaryExample();
        criteria = ex.createCriteria();
        criteria.andMdPidEqualTo(materialDictionary.getMdPid());
        criteria.andMdTypeNumEqualTo(materialDictionary.getMdTypeNum());
        criteria.andMdMatCodeEqualTo(materialDictionary.getMdMatCode());
        criteria.andMdStatusEqualTo(1);

        List<MMaterialDictionary> materialDictionary2 = mMaterialDictionaryDao.selectByExample(ex);

        if (materialDictionary2 != null && materialDictionary2.size() > 0) {
            return "编码：" + materialDictionary.getMdMatCode() + "在字典中已存在，请重新输入！";
        }

        //1板材、2型材、3通用
        String parentName = "";
        if (materialDictionary.getMdPid() == 1) {
            parentName = "【" + "板材" + "】";
        } else if (materialDictionary.getMdPid() == 2) {
            parentName = "【" + "型材" + "】";
        } else if (materialDictionary.getMdPid() == 3) {
            parentName = "【" + "通用" + "】";
        }

        //1000 类型、2000	材质、3000 规格
        String typeNum = "";
        if (materialDictionary.getMdTypeNum() == 1000) {
            typeNum = "【" + "类型" + "】";
        } else if (materialDictionary.getMdTypeNum() == 2000) {
            typeNum = "【" + "材质" + "】";
        } else if (materialDictionary.getMdTypeNum() == 3000) {
            typeNum = "【" + "规格" + "】";
        }

        materialDictionary.setMdStatus(1);
        logService.saveLog("创建原料数据字典", parentName + "->" + typeNum + ":" + materialDictionary.getMdName());
        mMaterialDictionaryDao.insert(materialDictionary);
        return "";
    }

    /**
     * 删除原料数据字典
     *
     * @param materialDictionary 被删除数据
     * @return
     */
    public String delMaterialDictionary(MMaterialDictionary materialDictionary) {

        // 判断：如果被原料引用，则提示不允许删除，只运行修改状态
        MMaterialsExample ex = new MMaterialsExample();
        MMaterialsExample.Criteria criteria = ex.createCriteria();
        criteria.andMatTypeIdEqualTo(materialDictionary.getMdId());
        criteria.andMatStatusEqualTo(1);

        MMaterialsExample.Criteria criteria2 = ex.createCriteria();
        criteria2.andMatQualityIdEqualTo(materialDictionary.getMdId());
        criteria.andMatStatusEqualTo(1);

        MMaterialsExample.Criteria criteria3 = ex.createCriteria();
        criteria3.andMatSpecIdEqualTo(materialDictionary.getMdId());
        criteria3.andMatStatusEqualTo(1);

        ex.or(criteria2);
        ex.or(criteria3);
        Long count = mMaterialsDao.countByExample(ex);

        if (count > 0) {
            return "该数据已经被原料所使用，不能删除！您可以先删除对应原料后再执行该操作！";
        }

        // 根据ID取得数据字典数据
        MMaterialDictionary mMaterialDictionary = mMaterialDictionaryDao.selectByPrimaryKey(materialDictionary.getMdId());

        //1板材、2型材、3通用
        String parentName = "";
        if (mMaterialDictionary.getMdPid() == 1) {
            parentName = "【" + "板材" + "】";
        } else if (mMaterialDictionary.getMdPid() == 2) {
            parentName = "【" + "型材" + "】";
        } else if (mMaterialDictionary.getMdPid() == 3) {
            parentName = "【" + "通用" + "】";
        }

        //1000 类型、2000	材质、3000 规格
        String typeNum = "";
        if (mMaterialDictionary.getMdTypeNum() == 1000) {
            typeNum = "【" + "类型" + "】";
        } else if (mMaterialDictionary.getMdTypeNum() == 2000) {
            typeNum = "【" + "材质" + "】";
        } else if (mMaterialDictionary.getMdTypeNum() == 3000) {
            typeNum = "【" + "规格" + "】";
        }

        mMaterialDictionary.setMdStatus(0);
        logService.saveLog("删除原料数据字典", parentName + "->" + typeNum + ":" + mMaterialDictionary.getMdName());
        mMaterialDictionaryDao.updateByPrimaryKeySelective(mMaterialDictionary);
        return "";
    }

    /**
     * 取得数据字典被原料引用次数
     *
     * @param mdId 字典主键
     * @return
     */
    public Long usedCount(Integer mdId) {

        MMaterialsExample ex = new MMaterialsExample();
        MMaterialsExample.Criteria criteria = ex.createCriteria();
        criteria.andMatTypeIdEqualTo(mdId);
        criteria.andMatStatusEqualTo(1);

        MMaterialsExample.Criteria criteria2 = ex.createCriteria();
        criteria2.andMatQualityIdEqualTo(mdId);
        criteria.andMatStatusEqualTo(1);

        MMaterialsExample.Criteria criteria3 = ex.createCriteria();
        criteria3.andMatSpecIdEqualTo(mdId);
        criteria3.andMatStatusEqualTo(1);

        ex.or(criteria2);
        ex.or(criteria3);

        return mMaterialsDao.countByExample(ex);
    }

    /**
     * 修改原料物料数据字典
     *
     * @param newMaterialDictionary 修改内容
     * @return
     */
    public String updateMaterialDictionary(MMaterialDictionary newMaterialDictionary) {

        MMaterialDictionary oldMaterialDictionary = mMaterialDictionaryDao.selectByPrimaryKey(newMaterialDictionary.getMdId());

        newMaterialDictionary.setMdMatCode(oldMaterialDictionary.getMdMatCode());
//        newMaterialDictionary.setMdPid(oldMaterialDictionary.getMdPid());
        newMaterialDictionary.setMdTypeNum(oldMaterialDictionary.getMdTypeNum());
        newMaterialDictionary.setMdStatus(1);

        // 检索条件创建
        MMaterialDictionaryExample ex = new MMaterialDictionaryExample();
        MMaterialDictionaryExample.Criteria criteria = ex.createCriteria();

        // 编码不允许重复
        if (newMaterialDictionary.getMdMatCode() != "") {

            criteria.andMdPidEqualTo(newMaterialDictionary.getMdPid());
            criteria.andMdTypeNumEqualTo(newMaterialDictionary.getMdTypeNum());
            criteria.andMdMatCodeEqualTo(newMaterialDictionary.getMdMatCode());
            criteria.andMdStatusEqualTo(1);
            criteria.andMdIdNotEqualTo(newMaterialDictionary.getMdId());

            List<MMaterialDictionary> materialDictionaryByCode = mMaterialDictionaryDao.selectByExample(ex);
            if (materialDictionaryByCode != null && materialDictionaryByCode.size() > 0) {
                return "编码在系统中已存在，请重新输入！";
            }
        }

        // 原料名称不允许重复
        ex.clear();
        ex = new MMaterialDictionaryExample();
        criteria = ex.createCriteria();
        criteria.andMdPidEqualTo(newMaterialDictionary.getMdPid());
        criteria.andMdTypeNumEqualTo(newMaterialDictionary.getMdTypeNum());
        criteria.andMdNameEqualTo(newMaterialDictionary.getMdName());
        criteria.andMdStatusEqualTo(1);
        criteria.andMdIdNotEqualTo(newMaterialDictionary.getMdId());

        List<MMaterialDictionary> materialDictionaryByName = mMaterialDictionaryDao.selectByExample(ex);
        if (materialDictionaryByName != null && materialDictionaryByName.size() > 0) {
            return "原料内容在系统中已存在，保存失败！";
        }

        logService.saveLog("修改原料数据字典信息", oldMaterialDictionary, newMaterialDictionary);
        mMaterialDictionaryDao.updateByPrimaryKeySelective(newMaterialDictionary);
        return "";
    }

    /**
     * 根据分类取得类型、材质、规格列表
     *
     * @param kindName
     * @return
     */
    public MMaterialDictionaryListModel findMaterialDictionaryByKindName(String kindName) {

        MMaterialDictionaryListModel materialDictionaryListModel = new MMaterialDictionaryListModel();

        List<Integer> list = new ArrayList<>();

        if ("板材".equals(kindName)) {
            list.add(1);
            list.add(3);
        } else if ("型材".equals(kindName)) {
            list.add(2);
            list.add(3);
        } else {
            list.add(1);
            list.add(2);
            list.add(3);
        }

        // 取得类型下拉选集合
        materialDictionaryListModel.setMaterialTypeList(findByStatusAndTypeNumAndParentIdInOrderByNameAsc(1, 1000, list));
        // 取得材质下拉选集合
        materialDictionaryListModel.setMaterialQualityList(findByStatusAndTypeNumAndParentIdInOrderByNameAsc(1, 2000, list));
        // 取得规格下拉选集合
        materialDictionaryListModel.setMaterialSpecList(findByStatusAndTypeNumAndParentIdInOrderByNameAsc(1, 3000, list));

        return materialDictionaryListModel;
    }

    /**
     * 根据分类名称获取类型、材质、规格下拉选内容
     *
     * @param status  数据状态
     * @param typeNum 类型
     * @param pIdList 父Id集合
     * @return
     */
    public List<MMaterialDictionary> findByStatusAndTypeNumAndParentIdInOrderByNameAsc(Integer status, Integer typeNum, List<Integer> pIdList) {

        MMaterialDictionaryExample ex = new MMaterialDictionaryExample();
        ex.setOrderByClause("md_name asc");
        MMaterialDictionaryExample.Criteria criteria = ex.createCriteria();

        criteria.andMdStatusEqualTo(status);
        criteria.andMdTypeNumEqualTo(typeNum);
        criteria.andMdPidIn(pIdList);

        return mMaterialDictionaryDao.selectByExample(ex);
    }


}
